home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libextra / popen.c < prev    next >
C/C++ Source or Header  |  1994-08-09  |  4KB  |  156 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <exec/types.h>
  5. #include <dos/dos.h>
  6. #include <dos/dostags.h>
  7. #ifdef __SASC
  8. #include <proto/dos.h>
  9. #endif
  10. #ifdef __GNUC__
  11. #include <inline/dos.h>
  12. #endif
  13.  
  14. #ifndef AMIGA
  15. static char template[] = "piXXXXXX";
  16. #else
  17. extern char *mktemp(char *);
  18. #endif
  19.  
  20. typedef enum { unopened = 0, reading, writing } pipemode;
  21. static
  22. struct {
  23.     char *command;
  24.     char *name;
  25.     pipemode pmode;
  26. } pipes[FOPEN_MAX];
  27.  
  28. static struct TagItem Tags[] = {
  29.   {NP_StackSize, 250000},
  30.   {TAG_DONE, NULL}
  31. };
  32.  
  33. FILE *
  34. popen(const char *command, const char *mode ) {
  35.     FILE *current;
  36.     char *name;
  37.     int cur;
  38.     pipemode curmode;
  39. #ifdef AMIGA
  40. char template[] = "T:piXXXXXX";
  41. #endif
  42.     /*
  43.     ** decide on mode.
  44.     */
  45.     if(strcmp(mode,"r") == 0)
  46.         curmode = reading;
  47.     else if(strcmp(mode,"w") == 0)
  48.         curmode = writing;
  49.     else
  50.         return NULL;
  51.     /*
  52.     ** get a name to use.
  53.     */
  54. #ifdef AMIGA
  55.     name = mktemp(template);
  56. #else
  57.     if((name = tempnam(".","pip"))==NULL)
  58.         return NULL;
  59. #endif
  60.     /*
  61.     ** If we're reading, just call system to get a file filled with
  62.     ** output.
  63.     */
  64.     if(curmode == reading) {
  65.         char cmd[256];
  66.         sprintf(cmd,"%s > %s",command,name);
  67.     SystemTagList(cmd, Tags);
  68.         if((current = fopen(name,"r")) == NULL)
  69.             return NULL;
  70.     } else {
  71.         if((current = fopen(name,"w")) == NULL)
  72.             return NULL;
  73.     }
  74.     cur = fileno(current);
  75.     pipes[cur].name = strdup(name);
  76.     pipes[cur].pmode = curmode;
  77.     pipes[cur].command = strdup(command);
  78.     return current;
  79. }
  80.  
  81. int
  82. pclose( FILE * current) {
  83.     int cur = fileno(current),rval;
  84.     /*
  85.     ** check for an open file.
  86.     */
  87.     if(pipes[cur].pmode == unopened)
  88.         return -1;
  89.     if(pipes[cur].pmode == reading) {
  90.         /*
  91.         ** input pipes are just files we're done with.
  92.         */
  93.         rval = fclose(current);
  94.         unlink(pipes[cur].name);
  95.     } else {
  96.         /*
  97.         ** output pipes are temporary files we have
  98.         ** to cram down the throats of programs.
  99.         */
  100.         char command[256];
  101.         fclose(current);
  102.         sprintf(command,"%s < %s",pipes[cur].command,pipes[cur].name);
  103.     rval = SystemTagList(command, Tags);
  104.         unlink(pipes[cur].name);
  105.     }
  106.     /*
  107.     ** clean up current pipe.
  108.     */
  109.     pipes[cur].pmode = unopened;
  110.     free(pipes[cur].name);
  111.     free(pipes[cur].command);
  112.     return rval;
  113. }
  114.  
  115. /* Special version of popen, to call the SAS/C preprocessor */
  116. FILE *
  117. popen2(const char *command) {
  118.     FILE *current;
  119.     char *name;
  120.     int cur;
  121.     char template[] = "T:piXXXXXX";
  122.     char cmd[256], tmpname[256];
  123.     char *fname, *fpart;
  124.     BPTR l;
  125.     /*
  126.     ** get a name to use.
  127.     */
  128.     name = mktemp(template);
  129.     /*
  130.     ** We're reading, just call system to get a file filled with
  131.     ** output.
  132.     */
  133.     fname = strrchr(command, ' ');
  134.     *fname++ = 0;
  135.     l = Lock(fname, ACCESS_READ);
  136.     if (!l) return NULL;
  137.     strcpy(tmpname, fname);
  138.     fpart = FilePart(tmpname);
  139.     *fpart = 0;
  140.     strcat(tmpname, "__ray__.c");
  141.     MakeLink(tmpname, l, 0);
  142.     UnLock(l);
  143.     sprintf(cmd,"%s OBJNAME %s %s",command,name,tmpname);
  144.     cmd[strlen(cmd)-2] = '\0';
  145.     SystemTagList(cmd, Tags);
  146.     DeleteFile(tmpname);
  147.     *--fname = ' ';
  148.     if((current = fopen(name,"r")) == NULL)
  149.     return NULL;
  150.     cur = fileno(current);
  151.     pipes[cur].name = strdup(name);
  152.     pipes[cur].pmode = reading;
  153.     pipes[cur].command = strdup(command);
  154.     return current;
  155. }
  156.